package com.hyh.prize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyh.prize.entity.Dict;
import com.hyh.prize.entity.TabGgcardObtain;
import com.hyh.prize.entity.TabSaleplanGiftcode;
import com.hyh.prize.entity.TabSaleplanGiftstock;
import com.hyh.prize.mapper.ObtainMapper;
import com.hyh.prize.service.*;
import com.hyh.prize.vo.Obtain;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ObtainServiceImpl extends ServiceImpl<ObtainMapper, TabGgcardObtain> implements ObtainService {

    @Resource
    private DictSerive dictSerive;
    @Resource
    private UserService userService;

    @Resource
    private GiftService giftService;
    @Resource
    private GgkService ggkService;

    @Resource
    private GgkStockService ggkStockService;
    /**
     * 查询用户参与获得的信息
     * @param uid
     * @return
     */
    public List<TabGgcardObtain> getList(String uid){
        QueryWrapper<TabGgcardObtain> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(uid)){
            queryWrapper.eq("SUBSCRBID",uid);
        }

        List<TabGgcardObtain> tabGgcardObtains = this.list(queryWrapper);
        for (TabGgcardObtain obtain : tabGgcardObtains) {
            //获取地区名称
            Dict dict1 = dictSerive.getById(obtain.getAreaid());
            obtain.setDName(dict1.getCodeName());
            //获取业务类型
            Dict d2 = dictSerive.getById(obtain.getSvcid());
            obtain.setYName(d2.getCodeName());
            //获取参与方式
            Dict d3 = dictSerive.getById(obtain.getJoinmode());
            obtain.setCShow(d3.getCodeName());
            //获取领取状态
            String d4 = dictSerive.get("1",obtain.getObtainstate());
            obtain.setStateName(d4);
            //营业厅名称
            Dict d5 = dictSerive.getById(obtain.getAcptsiteid());
            obtain.setYName(d5.getCodeName());
            //获取用户
            String nameById = userService.getNameById(obtain.getSubscrbid()+"");
            obtain.setName(nameById);
        }
        return tabGgcardObtains;
    }


    public String ixtis(String ggkId){
        String s = ggkService.exzhong(ggkId);
        return s;
    }
    /**
     * 根据中奖等级查询对应的礼品
     * @param level
     * @return
     */
    public List<TabSaleplanGiftcode> getGift(String level){
        List<TabSaleplanGiftcode> giftServiceGiftInfo = giftService.getGiftInfo(level);
        for (TabSaleplanGiftcode giftcode : giftServiceGiftInfo) {
            TabSaleplanGiftstock ggkStock = ggkStockService.getGgkStock(giftcode.getAcptsiteid(), giftcode.getGiftcode());
            if(ggkStock!=null){
                giftcode.setNumber(ggkStock.getStocknum()+"");
            }else{
                giftcode.setNumber("0");
            }

        }
        return giftServiceGiftInfo;
    }

    /**
     * 领取礼品
     * @param tabGgcardObtain
     * @return
     */
    public boolean receive(TabGgcardObtain tabGgcardObtain){
        //修改礼品库存信息
        boolean b = ggkStockService.receive(tabGgcardObtain.getLpid(), tabGgcardObtain.getObtainnum(), tabGgcardObtain.getAcptsiteid());

        //添加领取日志记录
        b = this.save(tabGgcardObtain);
        //修改刮刮卡状态
        b=ggkService.update(tabGgcardObtain.getGgkid());
        if(b){
            return true;
        }
        return false;

    }

}
